package com.ruoyi.system.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.EBusinessInfo;
import com.ruoyi.system.entity.BusinessInfo;
import com.ruoyi.system.entity.BusinessShareholder;
import com.ruoyi.system.service.QiXinBaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EBusinessShareholderMapper;
import com.ruoyi.system.domain.EBusinessShareholder;
import com.ruoyi.system.service.IEBusinessShareholderService;

/**
 * 工商股东信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-23
 */
@Service
public class EBusinessShareholderServiceImpl implements IEBusinessShareholderService
{
    @Autowired
    private EBusinessShareholderMapper eBusinessShareholderMapper;
    @Autowired
    QiXinBaoService qiXinBaoService;
    /**
     * 查询工商股东信息
     *
     * @param id 工商股东信息主键
     * @return 工商股东信息
     */
    @Override
    public EBusinessShareholder selectEBusinessShareholderById(Long id)
    {
        return eBusinessShareholderMapper.selectEBusinessShareholderById(id);
    }

    /**
     * 查询工商股东信息列表
     *
     * @param eBusinessShareholder 工商股东信息
     * @return 工商股东信息
     */
    @Override
    public List<EBusinessShareholder> selectEBusinessShareholderList(EBusinessShareholder eBusinessShareholder)
    {
        return eBusinessShareholderMapper.selectEBusinessShareholderList(eBusinessShareholder);
    }

    /**
     * 新增工商股东信息
     *
     * @param eBusinessShareholder 工商股东信息
     * @return 结果
     */
    @Override
    public int insertEBusinessShareholder(EBusinessShareholder eBusinessShareholder)
    {
        return eBusinessShareholderMapper.insertEBusinessShareholder(eBusinessShareholder);
    }

    /**
     * 修改工商股东信息
     *
     * @param eBusinessShareholder 工商股东信息
     * @return 结果
     */
    @Override
    public int updateEBusinessShareholder(EBusinessShareholder eBusinessShareholder)
    {
        return eBusinessShareholderMapper.updateEBusinessShareholder(eBusinessShareholder);
    }

    /**
     * 批量删除工商股东信息
     *
     * @param ids 需要删除的工商股东信息主键
     * @return 结果
     */
    @Override
    public int deleteEBusinessShareholderByIds(Long[] ids)
    {
        return eBusinessShareholderMapper.deleteEBusinessShareholderByIds(ids);
    }

    /**
     * 删除工商股东信息信息
     *
     * @param id 工商股东信息主键
     * @return 结果
     */
    @Override
    public int deleteEBusinessShareholderById(Long id)
    {
        return eBusinessShareholderMapper.deleteEBusinessShareholderById(id);
    }

    @Override
    public BusinessShareholder getBusinessShareholder(String discountApplicantCardCode,LoginUser loginUser) {
        EBusinessShareholder eBusinessShareholder=getBusinessShareholderByCodeScope(discountApplicantCardCode,loginUser);
        BusinessShareholder businessShareholder=new BusinessShareholder();
        if(null==eBusinessShareholder){
            eBusinessShareholder=new EBusinessShareholder();
            businessShareholder=qiXinBaoService.getBusinessShareholderByCode(discountApplicantCardCode,loginUser);
            if(StringUtils.isNotEmpty(businessShareholder.getStatus())&&"200".equals(businessShareholder.getStatus())){
                //请求启信包接口
                eBusinessShareholder.setBusinessCode(discountApplicantCardCode);
                eBusinessShareholder.setBusinessShareholderMsg(JSON.toJSONString(businessShareholder));
                eBusinessShareholder.setUserId(loginUser.getUserId());
                eBusinessShareholder.setDeptId(loginUser.getDeptId());
                insertEBusinessShareholder(eBusinessShareholder);
            }
        }else{
            businessShareholder=JSON.parseObject(eBusinessShareholder.getBusinessShareholderMsg(),BusinessShareholder.class);
        }
        return businessShareholder;
    }
    public EBusinessShareholder getBusinessShareholderByCodeScope(String discountApplicantCardCode, LoginUser loginUser){
        EBusinessShareholder eBusinessShareholder=new EBusinessShareholder();
        eBusinessShareholder.setBusinessCode(discountApplicantCardCode);
        eBusinessShareholder.setDeptId(loginUser.getDeptId());
      return  eBusinessShareholderMapper.getBusinessShareholderByCode(eBusinessShareholder);
    }
}
